package com.xbj.cloud.controller;

import com.xbj.cloud.commons.apis.Pay8001FeignApi;
import com.xbj.cloud.commons.resp.ResultData;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * @author: 观雾
 * @create: 2024-08-26
 * @description:
 **/
@RestController
@Tag(name = "熔断/舱壁测试模块",description = "熔断/舱壁测试类")
public class OrderCircuitController {
    private static final Logger LOG = LoggerFactory.getLogger(OrderCircuitController.class);

    @Resource
    private Pay8001FeignApi pay8001FeignApi;

    @GetMapping(value = "/orderCircuit/getById/{id}")
    @Operation(summary = "熔断测试方法",description = "-1:抛异常,-2:超时,其余正常")
    @CircuitBreaker(name = "pay8001", fallbackMethod = "myCircuitFallback")
    public ResultData orderCircuitGetById(@PathVariable(value = "id") Integer id){
        ResultData resultData = pay8001FeignApi.payCircuitGetById(id);
        return resultData;
    }
    //myCircuitFallback就是服务降级后的兜底处理方法
    public ResultData myCircuitFallback(Integer id,Throwable t) {
        // 这里是容错处理逻辑，返回备用结果
        return ResultData.success("myCircuitFallback，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~");
    }

    @GetMapping(value = "/orderBulkhead/getById/{id}")
    @Operation(summary = "舱壁测试方法",description = "-1:抛异常,-2:超时,其余正常")
    @Bulkhead(name = "pay8001",fallbackMethod = "myBulkheadFallback",type = Bulkhead.Type.SEMAPHORE)
    public ResultData orderBulkheadGetById(@PathVariable(value = "id") Integer id){
        ResultData resultData = pay8001FeignApi.payBulkheadGetById(id);
        return resultData;
    }
    public ResultData myBulkheadFallback(Throwable t)
    {
        return ResultData.success("myBulkheadFallback，隔板超出最大数量限制，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~");
    }


    @GetMapping(value = "/orderRatelimit/getById/{id}")
    @Operation(summary = "限流测试方法",description = "-1:抛异常,-2:超时,其余正常")
    @RateLimiter(name = "pay8001",fallbackMethod = "myRatelimitFallback")
    public ResultData orderRatelimitGetById(@PathVariable("id") Integer id)
    {
        return pay8001FeignApi.payBulkheadGetById(id);
    }
    public ResultData myRatelimitFallback(Integer id,Throwable t)
    {
        return ResultData.success("你被限流了，禁止访问/(ㄒoㄒ)/~~");
    }
}
